import java.util.Stack;

public class Code {



    public static int maxBracket(String string){
        char[] chars = string.toCharArray();
        Stack<Integer> integers = new Stack<>();
        int start = -1;
        int max = 0;
        for (int i = 0; i < chars.length; i++) {
            char aChar = chars[i];
            if(isLeft(aChar)){
                integers.push(i);
            }else if(integers.isEmpty()){//出现右括号匹配不上会进入这个
                    start = i;
            }else if(isMatch(chars[integers.pop()],aChar)){
                if(integers.isEmpty()){
                    max = Math.max(max,i-start);
                }else{
                    max = Math.max(max,i-integers.peek());
                }
            }else {//匹配不上把当前下标加入方便下次最长匹配括号计算
                integers.push(i);
            }
        }
        return max;
    }

    public  static boolean isLeft(char c){
        return '(' ==c || '['==c;
    }

    public static boolean isMatch(char c,char c2){
        return ((c=='(') && c2==')') ||(c=='['&& c2==']');
    }

    public static void main(String[] args) {
        System.out.println(maxBracket("(](())"));
    }

}
